package com.example.asus.zyfypt_tea_307plx.model;

import com.example.asus.zyfypt_tea_307plx.bean.TwareBean;
import com.example.asus.zyfypt_tea_307plx.common.common;
import com.example.asus.zyfypt_tea_307plx.iface.AllListeners;
import com.example.asus.zyfypt_tea_307plx.iface.Twareiface;
import com.example.asus.zyfypt_tea_307plx.service.TwareService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TwareModel implements Twareiface {
    private Retrofit retrofit;
    //构造函数
    public TwareModel()
    { //使用 Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getResultList(String mod, int page, String sessionID, final AllListeners<List<TwareBean>> listener) {
        //使用 Retrofit----2
        TwareService service
                =retrofit.create(TwareService.class);
        Call<List<TwareBean>> call
                =service.getTwareList(mod,page,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if (response.isSuccessful() && response != null) {
                    listener.onResponse(response.body());
                    System.out.println("##   成功1   ##");
                }
                else{
                    listener.onFail("on response fail");
                    System.out.println("##   失败2   ##");
                }

            }
            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                listener.onFail(t.toString()+ "AritcleModel--->50");
                System.out.println("##   失败3   ##");
            }
        });
    }
}
